package com.sky.mapper.user;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.pojo.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

public interface OrderMapper {
    Map<String, Long> selectEveryDayTotalByDateAndStatus(@Param("begin") String begin,
                                                         @Param("end") String end,
                                                         @Param("dateList") List<String> dateList,
                                                         @Param("status") Integer status);

    void save(Orders orders);

    @Update("update orders set status=#{status},pay_status=#{payStatus},checkout_time=#{checkoutTime} where number=#{number}")
    void updateOrderPaySuccess(Orders orders);


    List<Orders> selectOrdersByStatusAndTime(@Param("status") Integer status,
                                             @Param("maxTime") Date maxTime,
                                             @Param("minTime") Date minTime);

    @Update("update orders set status=#{status} where id=#{id}")
    void updateOrderStatusById(@Param("status") Integer status,
                               @Param("id") Long id);

    /**
     * 分页条件查询并按下单时间排序
     * @param ordersPageQueryDTO
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);


    /**
     * 根据id查询订单
     * @param id
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);


    /**
     * 订单搜索
     * @param dto
     * @return
     */
    Page<OrderVO> conditionSearch(OrdersPageQueryDTO dto);

    @Select("select count(*) from orders where status=#{status}")
    Integer selectCountByStatus(Integer status);

    @Select("SELECT  status, COUNT(*) total FROM orders GROUP BY STATUS")
    List<Map> selectAllStatusCount();

    OrderStatisticsVO selectAllStatusCount2();

    OrderVO adminOrderDetails(Long orderId);


    @Update("update orders set status=#{status},cancel_time=#{cancelTime},cancel_reason=#{cancelReason} where id=#{id}")
    void updateCancellingOrders(Orders orders);

    @Update("update orders set status=#{status} where number=#{outTradeNo}")
    void updateCancelOrderSuccess(@Param("status") Integer status, @Param("outTradeNo") String outTradeNo);

    @Update("update orders set status=#{status} where id=#{id}")
    void updateMerchantConfirmStatusById(Orders orders);

    @Update("update orders set status=#{status} where number=#{number}")
    void updateOrderStatusByNumber(@Param("number") String orderNumber,
                                   @Param("status") Integer status);

    @Update("update orders set status=#{status},delivery_time=#{deliveryTime} where number=#{number}")
    void updateStatusAndDeliveryTimeByNumber(@Param("status") Integer status,
                                             @Param("deliveryTime") LocalDateTime now,
                                             @Param("number") String orderNumber);

    @Select("SELECT * FROM orders WHERE number = (SELECT order_number FROM tb_courier_trade_order WHERE trade_no=#{tradeNo})")
    Orders selectOrdersByTradeNo(Long tradeNo);

    @Select("SELECT SUM(amount) FROM orders WHERE  order_time >= CONCAT(#{date},' 00:00:00') AND order_time<=CONCAT(#{date},' 23:59:59') AND STATUS=6")
    Double selectTotalAmountByDate(String date);

    Map<String, BigDecimal> selectAllDateTotalByDates(@Param("dateList") List<String> dateList,
                                                      @Param("begin") String begin,
                                                      @Param("end") String end);

    Long selectTotalByDateAndStatus(@Param("begin") String begin,
                                    @Param("end") String end,
                                    @Param("status") Integer status);


    int selectOrdersByDateAndStatus(@Param("begin") String beginStr,
                                    @Param("end") String endStr,
                                    @Param("status") Integer status);

    Map<String, Object> selectSalesVolumesByDay3(@Param("begin") String oldBegin,
                                                 @Param("end") String oldEnd,
                                                 @Param("list") List<String> dataList);


}
